package com.gcd.service.impl;

import com.gcd.mapper.ReserveCourseMapper;
import com.gcd.model.Course;
import com.gcd.model.Reserve;
import com.gcd.model.searchModel.CourseSearchModel;
import com.gcd.service.ReserveCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class ReserveCourseServiceImpl implements ReserveCourseService {
    private ReserveCourseMapper reserveCourseMapper;
    private RedisTemplate<String, Object> redisTemplate;

    private static final String COURSE_LIST_KEY = "course:list";

    @Autowired
    public void setReserveCourseMapper(ReserveCourseMapper reserveCourseMapper) {
        this.reserveCourseMapper = reserveCourseMapper;
    }
    @Autowired
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    // 查询课程
    @Override
    public List<CourseSearchModel> reserveCourse(CourseSearchModel course) {
        System.out.println(course);
        return reserveCourseMapper.reserveCourse(course);
    }

    // 查询课程列表
    @Override
    public List<String> courseList() {
        List<String> courseList=(List<String>) redisTemplate.opsForValue().get(COURSE_LIST_KEY);
        if (courseList==null){
            courseList=reserveCourseMapper.courseList();
            redisTemplate.opsForValue().set(COURSE_LIST_KEY,courseList);
        }
        return courseList;
    }

    // 添加预约记录
    @Override
    public boolean addReserveRecord(Reserve reserve) {
        return reserveCourseMapper.addReserveRecord(reserve);
    }

    // 查询预约记录
    @Override
    public Integer reserveRecord(Integer memberId,Integer courseId) {
        return reserveCourseMapper.reserveRecord(memberId,courseId);
    }

    //是否为VIP
    @Override
    public boolean isVip(Integer memberId) {
        return reserveCourseMapper.isVip(memberId)==0;
    }

}
